package io.babystar.modules.app.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.babystar.common.utils.DateUtils;
import io.babystar.common.utils.R;
import io.babystar.modules.business.entity.CourseDateEntity;
import io.babystar.modules.business.entity.ShopEntity;
import io.babystar.modules.business.entity.StudentEntity;
import io.babystar.modules.business.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.http.entity.ContentType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 个人中心
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-03-23 15:31
 */
@RestController
@RequestMapping("/app")
@Api("教练端")
public class AppTeacherController {

    @Autowired
    private StudentService studentService;
    @Autowired
    private SignService signService;
    @Autowired
    private BaseMemberCardService basememberCardService;
    @Autowired
    private ShopService shopService;
    @Autowired
    private CourseStudentService courseStudentService;
    @Autowired
    private CourseDateService courseDateService;
    @Autowired
    private StudentCourseDateService studentCourseDateService;

    private static final long FILE_MAX_SIZE = 1024 * 1024;
    private static final Logger log = LoggerFactory.getLogger(AppTeacherController.class);

    @GetMapping("/getStartTimeList")
    @ApiOperation("获取课时列表")
    public R getStartTimeList(String week, String shopId) {
        Map<String,Object> p = new HashMap<>();
        p.put("shopId", shopId);
        p.put("courseWeekZh", week);
        List<CourseDateEntity> list = courseDateService.queryCourseList(p);
        //时间去重
        list = removeDuplicate(list);
        Map<String, Object> map = new HashMap<>();
        map.put("startTimeList", list);
        return R.ok(map);
    }

    /**
     * 集合去重
     * @param list
     * @return
     */
    public  static  List<CourseDateEntity>  removeDuplicate(List<CourseDateEntity> list) {
        for (int i = 0; i < list.size() - 1; i++) {
            for (int j = list.size() - 1; j > i; j--) {
                CourseDateEntity one = list.get(j);
                CourseDateEntity two = list.get(i);
                if (one.getStartTime().equals(two.getStartTime())) {
                    list.remove(j);
                }
            }
        }
        return list;
    }

    @GetMapping("/studentList")
    @ApiOperation("签到列表")
    public R studentList(String week,String time,String keyword,String source,String shopId,String weekStr) {
        Map<String,String> p = new HashMap<>();
        p.put("week",week);
        p.put("time",time);
        p.put("source",source);
        p.put("shopId",shopId);
        p.put("studentname",keyword);
        p.put("weekstr",weekStr);
        List<Map<String, Object>> studentList = new ArrayList<>();
        try {
            studentList = studentService.getSignStudent(p);
            for (Map<String, Object> m : studentList) {
                boolean signStatus = (Boolean) m.get("sign_status");
                if (signStatus) {
                    m.put("hasSign", "active");
                } else {
                    m.put("hasSign", "");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error("数据获取失败");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("studentList", studentList);
        return R.ok(map);
    }

    /*@GetMapping("/studentOne")
    @ApiOperation("学员详情")
    public R studentOne(String id) {
        Map<String, Object> map = new HashMap<>();
        StudentEntity student = studentService.getById(id);
        if(student!=null) {
            MemberCardEntity member = memberCardService.selectById(student.getId());
            ShopEntity shop = shopService.getById(student.getShopid());

            //2.查询学生课程
            Map<String, Object> params = new HashMap<>();
            params.put("studentId", student.getId());

            //查询课程上课时间
            StudentCourseDateEntity scd = new StudentCourseDateEntity();
            scd.setStudentid(Integer.parseInt(student.getId()+""));
            List<CourseDateEntity> courseDateEntityList = studentCourseDateService.getStudentCourseDate(scd);
            map.put("courseDate", courseDateEntityList);

            map.put("card", member);
            map.put("shop", shop);
        }
        map.put("student", student);
        return R.ok(map);
    }*/


    @GetMapping("/getSysDate")
    @ApiOperation("判断是否已经签到")
    public R getSysDate(String signTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //系统时间
        Date nowDate = new Date();
        if(StringUtils.isNotEmpty(signTime)){
            try {
                Date signDate = sdf.parse(signTime+":00");
                long cha = (nowDate.getTime() - signDate.getTime()) / (1000 * 60);
                if(cha<60 && cha>-60){
                    return R.ok("true");
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return R.ok("false");
    }


    @GetMapping("/teacher/getWeek")
    @ApiOperation("获取周")
    public R getCourseWeek() {
        //取未来7天的日期 “2019-03-23 星期六”
        List<String> weekList = DateUtils.getFutureDate();
        return R.ok().put("weekList", weekList);
    }




}
